package cn.driver.common.tools;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/*
  统一响应模板
 */

/**
 * 统一响应模板
 */
public class ResponseData<T> implements Serializable {

    @ApiModelProperty("响应状态")
    private int code;
    @ApiModelProperty("响应消息")
    private String msg;
    @ApiModelProperty("响应数据")
    private T data;

    /**
     * 请求成功
     */
    public static <T> ResponseData<T> ok() {
        return restResult(null, HttpEnum.OK_200.code(), HttpEnum.OK_200.desc());
    }

    /**
     * 请求成功 返回数据
     */
    public static <T> ResponseData<T> ok(T t) {
        return restResult(t, HttpEnum.OK_200.code(), HttpEnum.OK_200.desc());
    }

    public static <T> ResponseData<T> ok(T t, String msg) {
        return restResult(t, HttpEnum.OK_200.code(), msg);
    }

    /**
     * 500 错误
     */
    public static <T> ResponseData<T> error() {
        return restResult(null, HttpEnum.ERROR_500.code(), HttpEnum.ERROR_500.desc());
    }


    /**
     * 500 错误
     */
    public static <T> ResponseData<T> error(String msg) {
        return restResult(null, HttpEnum.ERROR_500.code(), msg);
    }

    /**
     * 500 错误
     */
    public static <T> ResponseData<T> error(int code, String msg) {
        return restResult(null, code, msg);
    }

    /**
     * 502 请求失败
     */
    public static <T> ResponseData<T> FAIL_502() {
        return restResult(null, HttpEnum.FAIL_502.code(), HttpEnum.FAIL_502.desc());
    }

    /**
     * 600 非法参数
     */
    public static <T> ResponseData<T> error_600() {
        return restResult(null, HttpEnum.ERROR_600.code(), HttpEnum.ERROR_600.desc());
    }

    /**
     * 404
     */
    public static <T> ResponseData<T> notFound_404() {
        return restResult(null, HttpEnum.NOTFOUND_404.code(), HttpEnum.NOTFOUND_404.desc());
    }

    /**
     * 201操作成功
     */
    public static <T> ResponseData<T> ok_201() {
        return restResult(null, HttpEnum.HADNLE_201.code(), HttpEnum.HADNLE_201.desc());
    }

    /**
     * 201操作失败
     */
    public static <T> ResponseData<T> fail_201() {
        return restResult(null, HttpEnum.HANDLE_500.code(), HttpEnum.HANDLE_500.desc());
    }


    private static <T> ResponseData<T> restResult(T data, int code, String msg) {
        ResponseData<T> apiResult = new ResponseData<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
